package com.lgcns.andteam2.todaymenu;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.lgcns.andteam2.todaymenu.info.PlaceInfo;
import com.lgcns.andteam2.todaymenu.info.TodayInfo;
import com.lgcns.andteam2.todaymenu.info.WeekInfo;


/**
 * Tab을 관리하는 Activity
 */
public class MainActivity extends TabActivity {
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private TabHost mTabHost;
    private TodayInfo mTodayInfo;
    private WeekInfo mWeekInfo;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mTodayInfo = TodayInfo.getTodayInfo(this);
        mWeekInfo = WeekInfo.getWeekInfo(this);
        
        mTabHost = getTabHost();
        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                // 탭이 변경될 때 마다 title을 변경해준다.
                updateTitle(tabId);
            }
        });
        
        mTabHost.addTab(mTabHost.newTabSpec(Constants.TODAY_TAB_SPEC)
                .setIndicator(getString(R.string.today_tab), 
                              getResources().getDrawable(R.drawable.icon_1day))
                .setContent(new Intent(this, TodayActivity.class)));

        mTabHost.addTab(mTabHost.newTabSpec(Constants.WEEK_TAB_SPEC)
                .setIndicator(getString(R.string.week_tab), 
                              getResources().getDrawable(R.drawable.icon_7days))
                .setContent(new Intent(this, WeekActivity.class)));

        // 통지를 터치하여 들어오는 경우는 오늘메뉴(0번째) tab을 보여준다.
        Intent intent = getIntent();
        if(intent.getBooleanExtra(Constants.EXTRA_SHOW_TODAY, false)) {
            mTabHost.setCurrentTab(0);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        updateTitle(getTabHost().getCurrentTabTag());
    }

    /**
     * 현재 선택된 tab에 맞는 title을 지정한다.
     * @param tabTag
     */
    private void updateTitle(String tabTag) {
        if(Constants.TODAY_TAB_SPEC.equals(tabTag)) {
            mTodayInfo.refresh(this);
            setTitle(mTodayInfo.getDateText() + " " + PlaceInfo.getInstance(this).getPlace());
        } else {
            mWeekInfo.refresh(this);
            setTitle(mWeekInfo.getWeekText() + " "+ PlaceInfo.getInstance(this).getPlace());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.menu_item_settings:
                startActivity(new Intent(this, SettingActivity.class));
                return true;
            case R.id.menu_item_info:
                showAboutDialog();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void showAboutDialog() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View aboutView = inflater.inflate(R.layout.about, null);
        String title = getString(R.string.about_appl_name);
        String buttonMessage = getString(R.string.about_close_btn);
        
        new AlertDialog.Builder(this)
            .setTitle(title)
            .setIcon(R.drawable.icon)
            .setView(aboutView)
            .setNeutralButton(buttonMessage, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                // do nothing
              }
            }).show();
    }

}